<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
         request.setCharacterEncoding("UTF-8");
%>
<%@ page import="java.sql.*" %>

        
<html><body>
<%
Statement sql;
Connection con;
ResultSet rs;
   try{
       Class.forName("com.mysql.jdbc.Driver");
      }
       catch(Exception e){
    	   out.print("failed");
       }
       /* 
       public String handleString(String s){
    	   try{byte bb[] = s.getBytes("iso-8859-1");
    	   s=new String(bb);
    	   }
    	   catch(Exception ee){
    		   return s;
    	   } */
       
    try{ 
    		
    		String uri="jdbc:mysql://localhost:3306/sharing";
          
    	    Connection conn = DriverManager.getConnection(uri,"root","ZHUBI123.");
    	   String username = request.getParameter("username");
            String password = request.getParameter("password");
            String sex = request.getParameter("sex");
           String age = request.getParameter("age");
           String telephone = request.getParameter("telephone");
           String email = request.getParameter("email");
          String month = request.getParameter("month");
          String day = request.getParameter("day");
          String year = request.getParameter("year");
     
           String msg = null;
           sql=conn.createStatement() ;
           rs = sql.executeQuery("select * from student where username='" + username + "'"); //查询数据库
		//如果不存在
           if (!rs.next()) {
        	String birth;
             birth = year + "年" + month + "月" + day + "日";
          /*  byte[] bb=username.getBytes("iso-8859-1");
   	         username=new String(bb); 
   	      byte[] b=sex.getBytes("iso-8859-1");
	         sex=new String(b);  */
    		String updateCondition = "insert into student ( username, password, sex, birth, age, telephone , email )values('"+ username+ "','"+ 
    		password+ "','"+ sex+ "','"	+ birth+"','"+ 
    		age+ "','"+ telephone	+ "','"+ email + "')";
    		
            sql=conn.createStatement();
    		sql.executeUpdate(updateCondition); //插入数据库中
    		
            msg = "恭喜你，已注册成功！5秒后返回登录界面";
		out.println("<center><font color=green size=5>提示信息：<hr></font><font color=red size=4>"+ msg + "</font><hr>");
	    response.setHeader ("refresh", "5;URL=login.html");
            
    	}
		//如果注册已存在

           else {
            msg = "你所注册的用户已经存在，请你重新注册！";
    		out.println("<center><font color=green size=5>提示信息：<hr></font><font color=red size=4>"+ msg + "</font><hr>");
            out.println("<input type=button value=回上一页  onclick=history.back();>");}
      }     
       catch(Exception e){
    	   out.println("系统错误");
       };
            %>
            </body>
</html>
